package com.heatingcharge.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.heatingcharge.entity.sys.SysRoleMenu;
import com.heatingcharge.entity.sys.SysUser;
import com.heatingcharge.entity.sys.SysUserBuilding;
import com.heatingcharge.mapper.sys.SysUserBuildingMapper;
import com.heatingcharge.service.sys.ISysUserBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 收费人区域管理表 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2020-01-12
 */
@Service
public class SysUserBuildingServiceImpl extends ServiceImpl<SysUserBuildingMapper, SysUserBuilding> implements ISysUserBuildingService {

    @Override
    public List<Integer> getBuildingIdsByUserId(SysUserBuilding sysUserBuilding) {
        return baseMapper.selectBuildingIdsByUserId(sysUserBuilding);
    }

    @Override
    public Integer deleteByUserIdAndHeatYearId(Integer userId, Integer heatYearId) {
        QueryWrapper<SysUserBuilding> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("heat_year_id",heatYearId);
        return baseMapper.delete(wrapper);
    }

    @Override
    public Integer saveUserBuildings(Integer heatYearId, Integer userId, List<Integer> buildingIds) {
        deleteByUserIdAndHeatYearId(userId, heatYearId);

        if (buildingIds.size()>0){
            SysUserBuilding sysUserBuilding;
            List<SysUserBuilding> sysUserBuildings= Lists.newArrayList();
            for (int i=0;i<buildingIds.size();i++){
                sysUserBuilding=SysUserBuilding.builder()
                        .heatYearId(heatYearId)
                        .userId(userId)
                        .buildingId(buildingIds.get(i))
                        .build();
                sysUserBuildings.add(sysUserBuilding);
            }
            return this.saveBatch(sysUserBuildings) ?1:0;
        }
        return null;
    }
}
